package com.am.user.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/*
 * 发送短信
 *
 * */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SMSUtil {
    private String regionId;
    private String accessKeyId;
    private String accessKeySecret;
    private String signName;
    private String templateCode;

    public void sendMessage(String phoneNumbers,String param){
        System.out.println("regionId:"+regionId+","+"accessKeyId:"+accessKeyId+","
                +"accessKeySecret:"+accessKeySecret+","+"signName:"+signName+","+"templateCode:"+templateCode);
        DefaultProfile profile =
                DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setRegionId(regionId);
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName); // 短信签名
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\""+param+"\"}");

        SendSmsResponse acsResponse = null;
        try {
            acsResponse = client.getAcsResponse(request);
            System.out.println("短信发送成功");

        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }
}
